// pages/YouHuiJuan/YouHuiJuan.js
var util = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  data: {

    imgUrl: getApp().globalData.imgUrl,
    index: 0,
    gqImg: `background:url( ${getApp().globalData.imgUrl}jsy-newshopGuo.png) ;background-size:100% 100%`,
    mgqImg: `background:url( ${getApp().globalData.imgUrl}jsy-newshopMei.png) ;background-size:100% 100%`,
    arr: ['未使用', '已使用', '已失效'],
    page: 1,
    pageshow: 5,
    pageAdd: true,
    status0: [], // 未使用的空数组
    status1: [], // 已使用的空数组
    status2: [] // 已失效的空数组
  },
  click(e) { //点击导航
    var e = e.currentTarget.dataset.current
    this.setData({
      index: e
    })
  },
  getStatus() { // 获取 status
    let index = this.data.index,
      status;
    if (index == 0) { // 未使用
      status = 'all'
    } else if (index == 1) { // 已使用
      status = 0
    } else { // 已失效
      status = 1
    }
    return status
  },
  require(isBottom) {
    util.API('spwx/v1.shop/coupon_list', {
      status: this.getStatus(),
      page: this.data.page,
      pageshow: this.data.pageshow
    }).then(({
      code,
      data,
      msg
    }) => {
      if (isBottom) { // 触底
        if (code == 200) {
          let status,
            count;
          if (this.getStatus() == 'all') {
            status = 'status0'
            count = data.data.count0
          } else if (this.getStatus() == '0') {
            status = 'status1'
            count = data.data.count1
          } else if (this.getStatus() == '1') {
            status = 'status2'
            count = data.data.count2
          }
          if (count > 0) {
            this.setData({
              [status]: this.data[status].concat(data.data[status])
            })
          } else {
            this.setData({
              page: this.data.page,
              pageAdd: false
            })
          }
        } else {
          this.setData({
            pageAdd: false
          })
          wx.showToast({
            title: msg,
            icon: 'none'
          })
        }
      } else {
        if (code == 200) {
          this.setData({
            status0: data.data.status0,
            status1: data.data.status1,
            status2: data.data.status2
          })
        } else {
          this.setData({
              resData: []
            }),
            wx.showToast({
              title: msg + '',
              icon: 'none'
            })
        }
      }
    })
  },
  swiperChange(e) {
    this.setData({
      index: e.detail.current,
      page: 1,
      pageAdd: true
    })
  },
  bindscrolltolower(e) {
    if (this.data.pageAdd) {
      this.setData({
        page: this.data.page + 1
      })
    }
    this.require(true)
  },
  use(e) {
    let json = e.currentTarget.dataset.coupon
    wx.navigateTo({
      url: `../ShopList/ShopList?coupon=${JSON.stringify(json)}&title=可用商品`
    })
  },
  onLoad: function(options) {
    console.log(options)
    this.data.status = options.status
  },
  onShow() {
    this.setData({
      page: 1
    })
    this.require()
  }
}))